package com.hk.controller;

import com.alibaba.fastjson.JSON;
import com.hk.commons.CommonResult;
import com.hk.model.Admin;
import com.hk.model.vo.Result;
import com.hk.service.IAdminService;
import com.hk.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/1 11:09
 * @Statement:
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService iAdminService;

    private Jedis jedis = JedisUtil.getJedis();

    @GetMapping("/tologin")
    public Result tologin(Admin admin, HttpSession session) {
        Admin admin1 = iAdminService.findByEmailAndPassword(admin);
        if (admin1.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        //            存储登录信息(Session)
//        session.setAttribute("admin",admin1);
//        redis存储登录成功信息
        jedis.set("admin", JSON.toJSONString(admin1));
        Result result = CommonResult.successResult(admin1);
        return result;
    }

    @GetMapping("/loginAdmin")
    public Result loginAdmin(HttpSession session) {
//        获取Session中的登录信息
        /*Admin admin1 = (Admin) session.getAttribute("admin");
        String id = session.getId();
        System.out.println("JSessionID=="+id);*/
//        redis获取登录成功信息
        String admin = jedis.get("admin");
        if (admin == null) {
            Result result = CommonResult.errorResult(null);
            return result;
        }
        Admin admin1 = JSON.parseObject(admin, Admin.class);
        Result result = CommonResult.successResult(admin1);
        return result;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        Admin admin = iAdminService.findById(id);
        if (admin.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(admin);
        return result;

    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody Admin admin) {
        int update = iAdminService.update(admin);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Admin admin) {
        int update = iAdminService.updatePassWord(admin);
        Result result = CommonResult.successResult(null);
        return result;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/destroyLogin")
    public Result destroyLogin(HttpSession session) {
//        Session删除登录信息
//        session.removeAttribute("admin");
        Long del = jedis.del("admin");
        if (del < 1) {
            Result result = CommonResult.errorResult(null);
            return result;
        }
        System.out.println("退出登录");
        Result result = CommonResult.successResult(null);
        return result;

    }


}
